package com.xy.user.base.service;


import com.xy.core.init.mysql.BaseEntity;
import com.xy.core.page.Pagination;
import com.xy.user.base.persistence.BaseMapper;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 汤伟杰 on 2017/6/2.
 */
public class BaseService<T extends BaseEntity> {

    public Map<String,Object> getResult(Integer status,String code,String msg,Object data){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("status", status);
        result.put("code", code);
        result.put("msg", msg);
        result.put("data", data);
        return result;
    }

    protected BaseMapper<T> baseMapper;

    public BaseMapper<T> getBaseMapper() {
        return baseMapper;
    }

    public void setBaseMapper(BaseMapper<T> baseMapper) {
        this.baseMapper = baseMapper;
    }

    public Pagination list(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) throws Exception {

        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = baseMapper.getTotalRows(params);
        List<Map<String, Object>> list = baseMapper.getList(params);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;
    }
    public Pagination apilist(int pageNo, int pageSize, Map<String, Object> params, ModelMap model) {
        Pagination pagination = new Pagination();
        pagination.setPageNo(pageNo);
        pagination.setPageSize(pageSize);
        params.putAll(pagination.toMap());
        int count = baseMapper.getApiTotalRows(params);
        List<Map<String, Object>> list = baseMapper.getApiList(params);
        pagination.setTotalCount(count);
        pagination.setList(list);
        return pagination;
    }
    public List<Map<String,Object>> getApiList(Map<String, Object> params){
        return baseMapper.getApiList(params);
    }
    public Map<String,Object> getApiDetail(Map<String, Object> params) {


        return baseMapper.getApiDetail(params);

    }


    public void insert(T t){
        baseMapper.insert(t);
    }

    public void update(T t){
        baseMapper.update(t);
    }

    public void deleteById(String commentPicId){
        baseMapper.deleteById(commentPicId);
    }

    public void deleteByIds(List<String> list){
        baseMapper.deleteByIds(list);
    }

    public T queryById(String id){
        return baseMapper.queryById(id);
    }

    public List<T> queryByIds(List<String> list){
        List<T> list2 = baseMapper.queryByIds(list);
        if(list2==null){
            list2 = new ArrayList<T>();
        }
        return list2;
    }

    public void saves(List<T> list){
        if(list!=null&&list.size()>0){
            baseMapper.saves(list);
        }

    }

    public List<Map<String, Object>> getListBy(Map<String, Object> params){
        return baseMapper.getList(params);
    }
    public T queryByOne(Map<String, Object> params){
        return baseMapper.queryByOne(params);
    }
}
